/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.nViron.Tiles;

import instar.dg.Packets.Channel;
import instar.dg.Tiles.TilesCache;
import instar.dg.Tiles.TilesCacheMem;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.nViron.Graffiti.GraffitiClient;

/**
 *
 * @author Stefan
 */
public class TilesCacheMemorySE extends TilesCacheMem {

    protected TilesCacheMemorySE( String serverIP, int port ) {
        super( serverIP, port );
    }

    public static final void init( String serverIP, int port ) {
        TilesCache.instance = new TilesCacheMemorySE( serverIP, port );
    }

    @Override
    public Channel openChannel() throws IOException {

        Channel channel = null;
        try {
            Socket sock = new java.net.Socket(serverIP, port);
            sock.setSoLinger(true, 5);
            sock.setKeepAlive(true);

          channel = new Channel(sock.getInputStream(), sock.getOutputStream());
        } catch (UnknownHostException ex) {
            Logger.getLogger(GraffitiClient.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(GraffitiClient.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

        return channel;
    }

}
